<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" :align-center="true" width="50%" title="提现申请">
    <el-form
      ref="dealerCashFormRef"
      :rules="rules"
      :disabled="dialogData.isView"
      :model="dialogData.itemInfo"
      label-width="100px"
      label-suffix=" :"
      :hide-required-asterisk="dialogData.isView"
    >
      <el-form-item label="提现金额" prop="payAmt">
        <el-input v-model="dialogData.itemInfo.payAmt" placeholder="请填写提现金额" maxlength="7" clearable></el-input>
      </el-form-item>
      <el-form-item label="到帐卡号" prop="payUserBankId">
        <el-select v-model="dialogData.itemInfo.payUserBankId" placeholder="请选择到帐卡号" style="width: 100%">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="提现备注">
        <el-input
          v-model="dialogData.itemInfo.payNote"
          placeholder="请选择提现备注"
          type="textarea"
          maxlength="100"
          :rows="4"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="手机号" prop="payPhone">
        <el-input
          v-model="dialogData.itemInfo.payPhone"
          placeholder="请填写手机号"
          maxlength="20"
          show-word-limit
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="phoneCode">
        <el-input v-model="dialogData.itemInfo.phoneCode" maxlength="10" placeholder="请填写验证码" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" v-show="!dialogData.isView" @click="handleSubmit" :loading="btnLoading">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { OptEnum } from "@/biz/eenum/appEnum";
import { DialogParam } from "@/biz/interface/DialogType";
import { DealerCashEntity } from "@/biz/interface/modules/DealerCashType";
import { getDealerCashInfoApi, saveDealerCashApi } from "@/api/modules/dealer/cash";
import { getUserBankDropsApi } from "@/api/modules/dealer/card";

//页面的数据
interface DealerCashDialogOption extends DialogParam {
  //弹出表单的标题
  title: string;
  //是否是查看状态，查看状态是不可以提交数据
  isView: boolean;
  //当前的数据对象
  itemInfo: DealerCashEntity;
  //其它的数据都继续在这里加
}
const options = ref([] as any);
//提交按钮，避免多次提交
const btnLoading = ref(false);
//弹出框的参数
const dialogVisible = ref(false);
//当前表单数据在这里初始化
const dialogData = reactive<DealerCashDialogOption>({
  optType: OptEnum.UNKNOW,
  title: "",
  isView: false,
  itemInfo: {
    payUserBankId: "",
    payNote: "",
    payPhone: "",
    phoneCode: ""
  }
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogParam): void => {
  dialogData.optType = params.optType;
  dialogData.rowData = params.rowData;
  dialogData.isView = dialogData.optType === OptEnum.VIEW;

  if (dialogData.optType === OptEnum.CREATE) {
    initItemInfo();
  } else if (dialogData.rowData && dialogData.rowData.id) {
    getItemInfo(dialogData.rowData.id);
  }

  dialogVisible.value = true;
};

//初始化页面的数据
const initItemInfo = (): void => {
  dialogData.itemInfo = {
    payUserBankId: "",
    payNote: "",
    payPhone: "",
    phoneCode: ""
  };
};
//获取当前数据的详情，直接调用接口获取数据
const getItemInfo = async (id: string) => {
  if (id) {
    let { data } = await getDealerCashInfoApi({ id });
    console.log("接收到的数据", data);
    dialogData.itemInfo = data as unknown as DealerCashEntity;
  }
};

//数据较验
const rules = reactive({
  payAmt: [{ required: true, message: "请填写提现金额", trigger: "blur" }],
  payUserBankId: [{ required: true, message: "请选择到帐卡号", trigger: "blur" }],
  payPhone: [{ required: true, message: "请填写手机号", trigger: "blur" }],
  phoneCode: [{ required: true, message: "请填写验证码", trigger: "blur" }]
});

// 获取所有的用户绑定的银行卡
const getUserBankDrops = (): void => {
  getUserBankDropsApi().then(res => {
    options.value = res?.data || [];
  });
};

// 提交数据（新增/编辑）
const dealerCashFormRef = ref<FormInstance>();
const handleSubmit = () => {
  btnLoading.value = true;
  dealerCashFormRef.value!.validate(async valid => {
    if (!valid) {
      btnLoading.value = false;
      return;
    }
    try {
      if (dialogData.optType === OptEnum.CREATE || dialogData.optType === OptEnum.EDIT) {
        //提交数据
        let res = await saveDealerCashApi({
          ...dialogData.itemInfo
        });
        if (res.data) {
          ElMessage.success({ message: `${dialogData.title}dealerCash成功！` });
          //刷新列表数据
          emits("refresh-list");
          dialogVisible.value = false;
        }
      }
    } catch (error) {
    } finally {
      btnLoading.value = false;
    }
  });
};

//初始化一些参数
onMounted(() => {
  //   console.log("加载数据");
  getUserBankDrops();
});

//页面事件
const emits = defineEmits(["refresh-list"]);

//页面参数
defineExpose({
  acceptParams
});
</script>
